.PHONY: all clean clean_obj lib fsimage

CCFLAGS	:= -gstabs -I../inc -I../lib -O0 -nostdlib -nostdinc -fno-builtin # -DU_HEAP_DEBUG

LIBGCC	:= $(shell ${CC} -print-libgcc-file-name)

HEADERS := ulib.h ulayout.h va.h ips.h ipc.h process.h tb.h pcireg.h pci.h file.h fs.h
OBJFILES:= ulib.S.o entry.S.o entry.o va.o ips.o ipc.o process.o tb.o pci.o file.o

OBJDUMP := objdump

all: user_image.S

bin2S: bin2S.c
	${CC} $< -o $@

ramfs_export: ramfs_export.c
	${CC} -O0 -g $< -o $@

ramfs_image: ramfs_export ramfs_list
	./ramfs_export < ramfs_list >$@

ramfs_image.S: ramfs_image bin2S
	./bin2S ramfs_image $@

user_image.S: user user.sym user.asm bin2S
	./bin2S $< $@

lib:
	${MAKE} -C ../lib/

user: lib ${OBJFILES}
	${LD} -N -T user.ld -o $@ ${OBJFILES} ../lib/*.o ${LIBGCC}

user.sym: user
	nm $< | grep -i " t " | sed "s/ [tT] / /g" > $@

user.asm: user
	${OBJDUMP} -S $< > $@

ulib.S.o: ulib.S ramfs_image.S
	${CC} ${CCFLAGS} $< -c -o$@

entry.S.o: entry.S
	${CC} ${CCFLAGS} $< -c -o$@

%.o: %.c ${HEADERS}
	${CC} ${CCFLAGS} $< -c -o$@

clean_obj:
	-${RM} ${OBJFILES}

clean: clean_obj
	-${RM} user.sym
	-${RM} user.asm
	-${RM} user
	-${RM} bin2S
	-${RM} ramfs_export
	-${RM} ramfs_image
	-${RM} user_image.S
	-${RM} ramfs_image.S

